package jdbc1;

import org.junit.Test;

import java.sql.*;

public class JdbcStudy {


        private String url="jdbc:mysql://localhost:3306/studnt?useSSL=false&serverTimezone=UTC";
        private String user="root";
        private String passWord="123456";

        public JdbcStudy() {

        }

    public JdbcStudy(String passWord, String user, String url) {
        this.passWord = passWord;
        this.user = user;
        this.url = url;
    }
    public  Connection conn() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url,user,passWord);
            System.out.println("conn = " + conn);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }
    public static int stateObj(Connection conn, String sql) {
        int i = 0;
        try {
            Statement statement = conn.createStatement();
            i = statement.executeUpdate(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }
    public void findAllDate(String sql) throws SQLException {
            Connection conn=conn();
        Statement statement = conn.createStatement();
        System.out.println("statement = " + statement);
        ResultSet resultSet = statement.executeQuery(sql);
        System.out.println("resultSet = " + resultSet);
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String username = resultSet.getString("username");
            String password = resultSet.getString("password");
            System.out.println(id + " | " + username + " | " + password);
        }
        statement.close();
        conn.close();
    }

    public boolean isAdmin(String admin,String pwd) throws SQLException {
            Connection conn=conn();
            Statement statement= conn.createStatement();
            String sql="select*from st where admin="+admin+"and password"+pwd;
            ResultSet resultSet=statement.executeQuery(sql);
            return resultSet.next();
    }


    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection  conn= DriverManager.getConnection
                ("jdbc:mysql://localhost:3306/tbl_student?useSSL=false&serverTimezone=UTC",
                        "root","123456");
        String sql="select * from studnt";
        Statement statement=conn.createStatement();
        System.out.println(statement);
        ResultSet resultSet=statement.executeQuery(sql);
        System.out.println(resultSet);
        while (resultSet.next()){
            int id=resultSet.getInt("stu_id");
            System.out.println(id);
        }
    }
}
